﻿package mortal.game.view.chat.chatPrivate
{
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.events.*;

    public class ChatIconBase extends View
    {
        protected var _targetWindow:ChatBaseWindow;
        protected var _bg:GBitmap;
        protected var _iconImg:GImageBitmap;
        protected var _lastTime:int;

        public function ChatIconBase()
        {
            this.layer = LayerManager.chatIconLayer;
            this.create();
            this.addEvent();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            return;
        }// end function

        protected function create() : void
        {
            this._bg = UIFactory.gBitmap(ImagesConst.FriendHeadBg, 0, 0, this);
            this._iconImg = UIFactory.gImageBitmap("", 3, 3, this);
            this.mouseEnabled = true;
            return;
        }// end function

        protected function addEvent() : void
        {
            this.addEventListener(MouseEvent.CLICK, this.onClick);
            return;
        }// end function

        protected function onClick(event:MouseEvent) : void
        {
            if (this._targetWindow)
            {
                if (this._targetWindow.isHide)
                {
                    this._targetWindow.show();
                }
                else
                {
                    this._targetWindow.hide();
                }
            }
            return;
        }// end function

        public function updateChatTarget(param1) : void
        {
            return;
        }// end function

        public function blink() : void
        {
            Global.instance.addEnterFrame(this.onEnterFrame);
            return;
        }// end function

        protected function onEnterFrame() : void
        {
            if (FrameManager.flashFrame.getTime() - this._lastTime > 500)
            {
                this._lastTime = FrameManager.flashFrame.getTime();
                this.alpha = this.alpha == 1 ? (0) : (1);
            }
            return;
        }// end function

        protected function stopBlink() : void
        {
            Global.instance.removeEnterFrame(this.onEnterFrame);
            this.alpha = 1;
            return;
        }// end function

        public function set targetWindow(param1:ChatBaseWindow) : void
        {
            if (this._targetWindow)
            {
                this._targetWindow.removeEventListener(WindowEvent.SHOW, this.onTargetWindowShow);
            }
            this._targetWindow = param1;
            this._targetWindow.addEventListener(WindowEvent.SHOW, this.onTargetWindowShow);
            return;
        }// end function

        protected function onTargetWindowShow(event:Event) : void
        {
            if (this.isHide)
            {
                this.showNew(0, 0, false);
            }
            this.stopBlink();
            return;
        }// end function

        public function getMsg() : void
        {
            if (this._targetWindow && this._targetWindow.isHide)
            {
                this.blink();
            }
            this.showNew(0, 0, true);
            return;
        }// end function

        public function showNew(param1:int = 0, param2:int = 0, param3:Boolean = true) : void
        {
            if (_layer)
            {
                _isHide = false;
                _layer.addPopUp(this, !param3);
                dispatchEvent(new ViewEvent(ViewEvent.Show));
                if (param1 != 0 && param2 != 0)
                {
                    _layer.setPosition(this, param1, param2);
                }
            }
            return;
        }// end function

        override public function hide() : void
        {
            this.alpha = 1;
            super.hide();
            return;
        }// end function

    }
}
